package org.iku.common;

import java.io.File;
import java.io.IOException;
import java.util.TimerTask;

import org.apache.log4j.Logger;
import org.apache.lucene.document.Document;
import org.apache.lucene.index.CorruptIndexException;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.index.LogByteSizeMergePolicy;
import org.apache.lucene.index.LogMergePolicy;
import org.apache.lucene.index.Term;
import org.apache.lucene.store.FSDirectory;
import org.apache.lucene.store.LockReleaseFailedException;


public abstract class BasicIndexBuilder {
	private Logger logger = Logger.getLogger(BasicIndexBuilder.class);
	
	public abstract void build();
	
	public IndexWriter init(IndexWriterConfig config,FSDirectory directory,File file){
		try {
			LogMergePolicy mergePolicy = new LogByteSizeMergePolicy();
			mergePolicy.setMergeFactor(3000);
			mergePolicy.setUseCompoundFile(true);
			config.setMaxBufferedDocs(10000);
			mergePolicy.setMaxMergeDocs(10000);
			config.setMergePolicy(mergePolicy);
			config.setOpenMode(IndexWriterConfig.OpenMode.CREATE_OR_APPEND);
			directory = FSDirectory.open(file);
			if(IndexWriter.isLocked(directory)){
				try {
					IndexWriter.unlock(directory);
				} catch (LockReleaseFailedException e) {
					directory.clearLock(IndexWriter.WRITE_LOCK_NAME);
				}
			}
			return new IndexWriter(directory, config);
		} catch (Exception e) {
			logger.info(e.getMessage());
			
		}
		return null;
	}
	
}
